package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.SysDictDataMapper;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.SysDictDataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 添加字典数据
     * @param sysDictDataVo
     */
    @Override
    @Transactional
    public void add(SysDictDataVo sysDictDataVo) {
      if (sysDictDataVo==null){
          throw new RuntimeException("添加失败");
      }
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataVo,sysDictData);
        sysDictData.setUid(UuidUtil.getUuid());
        sysDictData.setCreateTime(new Date());
        sysDictData.setUpdateTime(new Date());
        //TODO 创建人uid
        sysDictData.setCreateByUid("1f01cd1d2f474743b241d74008b12333");
        //TODO 更新人uid
        sysDictData.setUpdateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictDataMapper.insertSelective(sysDictData);
    }

    /**
     * 批量删除字典数据
     * @param sysDictDataVoList
     */
    @Override
    @Transactional
    public void deleteBatch(List<SysDictDataVo> sysDictDataVoList) {
        if (sysDictDataVoList==null){
            throw new RuntimeException("字典数据不存在");
        }
        for (SysDictDataVo sysDictDataVo : sysDictDataVoList) {
            SysDictData sysDictData = new SysDictData();
            BeanUtils.copyProperties(sysDictDataVo,sysDictData);
            int i = sysDictDataMapper.deleteByPrimaryKey(sysDictData);
            if (i<=0){
                throw new RuntimeException("字典数据不存在");
            }
        }
    }

    @Override
    public void edit(SysDictDataVo sysDictDataVo) {
        if (sysDictDataVo==null){
            throw new RuntimeException("该数据不存在");
        }
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataVo,sysDictData);
        sysDictData.setUpdateTime(new Date());
        sysDictData.setUpdateByUid("1723ad77a1ca06b29a011471036a2dd6");
        int i = sysDictDataMapper.updateByPrimaryKeySelective(sysDictData);
        if (i<=0){
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public PageInfo list(SysDictDataVo sysDictDataVo) {
        if (sysDictDataVo==null){
            return new PageInfo(sysDictDataMapper.selectAll());
        }
        if (sysDictDataVo.getCurrentPage()!=null&&sysDictDataVo.getPageSize()!=null){
            PageHelper.startPage(sysDictDataVo.getCurrentPage(),sysDictDataVo.getPageSize());
        }
        Example example = new Example(SysDictData.class);
        Example.Criteria criteria = example.createCriteria();
        if (sysDictDataVo.getDictLabel()!=null&&!"".equals(sysDictDataVo.getDictLabel())){
            criteria.orLike("dictLabel","%"+sysDictDataVo.getDictLabel()+"%");
        }
        if (sysDictDataVo.getStatus()!=null){
            criteria.orEqualTo("status",sysDictDataVo.getStatus());
        }
        List<SysDictData> sysDictData = sysDictDataMapper.selectByExample(example);

        return new PageInfo(sysDictData);
    }

    @Override
    public PageInfo listBDT(String dicType) {
        List<SysDictData> sysDictDataList = sysDictDataMapper.findBydicType(dicType);
//TODO 部分数据封装不进去
        return new PageInfo(sysDictDataList);
    }

    @Override
    public PageInfo listBDTL(List<String> dicTypeList) {
       if (dicTypeList==null&&dicTypeList.size()<=0){
           throw new RuntimeException("字典类型为空");
       }
       List<List> lists=new ArrayList<>();
        for (String s : dicTypeList) {
            List<SysDictData> bydicType = sysDictDataMapper.findBydicType(s);
            lists.add(bydicType);
        }
    return new PageInfo(lists);
    }
}
